﻿using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;

namespace CMS.REST
{
    [JsonArray]
    public class RESTWhere : IEnumerable<object>
    {
        /// <summary>
        /// 读取或设置筛选字段
        /// </summary>
        [JsonIgnore]
        public string Field { get; set; }

        /// <summary>
        /// 读取或设置运算符
        /// </summary>
        [JsonIgnore]
        public string Operator { get; set; } = "=";

        /// <summary>
        /// 读取或设置目标值
        /// </summary>
        [JsonIgnore]
        public object Value { get; set; }

        /// <summary>
        /// 初始化一个空的条件
        /// </summary>
        public RESTWhere()
        {

        }

        /// <summary>
        /// 初始化一个Where条件
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public RESTWhere(string field, object value)
        {
            Field = field;
            Value = value;
        }

        /// <summary>
        /// 初始化一个Where条件
        /// </summary>
        /// <param name="field"></param>
        /// <param name="op"></param>
        /// <param name="value"></param>
        public RESTWhere(string field, string op, object value)
        {
            Field = field;
            Operator = op;
            Value = value;
        }

        public IEnumerator<object> GetEnumerator()
        {
            yield return Field;
            yield return Operator;
            yield return Value;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            yield return Field;
            yield return Operator;
            yield return Value;
        }
    }
}
